﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using JXWebSite.Models;
using JX.Application;
using JX.Infrastructure.Common;
using JX.Infrastructure;
using JX.Core;
using JX.Core.Entity;

namespace JXWebSite.Controllers
{
	/// <summary>
	/// 网站首页控制器
	/// </summary>
	public class HomeController : Controller
	{
		public ILogger<HomeController> _logger { get; set; }
		private readonly IRegionServiceAppDapper _regionServiceApp;
		/// <summary>
		/// 
		/// </summary>
		/// <param name="regionServiceApp"></param>
		public HomeController(IRegionServiceAppDapper regionServiceApp)
		{
			_regionServiceApp = regionServiceApp;
		}

		public IActionResult Index()
		{
			ViewBag.ConsignmentFormat = ConfigHelper.Get<ShopTemplateConfig>().ConsignmentFormat;
			_logger.LogInformation("访问index");
			return View();
		}

		public IActionResult Privacy()
		{
			var result = _regionServiceApp.Get(m => m.RegionID == 10);
			var resultList = _regionServiceApp.GetList(m => m.RegionID >= 10 && m.RegionID <= 15, m => m.OrderBy(item => item.RegionID));
			string msg = result.ToJson();
			resultList.ToList().ForEach(item =>
			{
				msg += item.ToJson();
			});
			return Json(msg);
		}

		public IActionResult Category(string id)
		{
			if (string.IsNullOrEmpty(id))
			{
				return RedirectToAction(nameof(HomeController.Error), "Home");
			}
			var arrNodeID = id.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
			int nodeID = DataConverter.CLng(arrNodeID[0]);
			int pageNum = 0;
			if (nodeID <= 0)
			{
				return RedirectToAction(nameof(HomeController.Error), "Home");
			}
			if (arrNodeID.Length == 2)
			{
				pageNum = DataConverter.CLng(arrNodeID[1]);
			}
			ViewBag.NodeID = "NodeID:" + nodeID;
			ViewBag.PageNum = "PageNum:" + pageNum;
			ViewData["Title"] = "栏目标题";
			return View();
		}

		public IActionResult ModelField()
		{
			return View();
		}

		[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
		public IActionResult Error()
		{
			return View();
		}
	}
}
